package team.first.moiveapiv1.controller;

import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import team.first.moiveapiv1.domain.User;
import team.first.moiveapiv1.service.UserService;
import team.first.moiveapiv1.vo.ApiResponse;

/**
 * @Author: HAPPY
 * @Project_name: project
 * @Package_name: team.first.moiveapiv1.controller
 * @Date: 2021/11/25 19:21
 * @Description:
 */
@RestController
public class UserController {
    @Autowired
    UserService userService;

    /**
     * 2.1 POST 用户注册接口
     * @param user 要注册的用户的部分信息
     * @return 返回注册后的用户的全部信息，也就只是多个个自增id
     */
    @ApiOperation("2.1 POST 用户注册接口")
    @PostMapping("/api/v1/register")
    public ApiResponse<User> register(@ApiParam("要注册的用户信息") User user) {
        ApiResponse<User> apiResponse = new ApiResponse<>();

        try {
            userService.register(user);
            apiResponse.setCode(200).setSuccess(true).setMessage("请求成功").setData(user);
        } catch (Exception e) {
            apiResponse.setCode(500).setSuccess(false).setMessage("请求失败");
            e.printStackTrace();
        }
        return apiResponse;
    }

    /**
     * 2.2 POST 用户登录接口
     * @param user 登录的用户部分信息
     * @return 返回登录后的用户全部信息
     */
    @ApiOperation("2.2 POST 用户登录接口")
    @PostMapping("/api/v1/login")
    public ApiResponse<User> login(User user) {
        ApiResponse<User> apiResponse = new ApiResponse<>();

        try {
            User loginUser = userService.login(user);

            apiResponse.setCode(200).setSuccess(true).setMessage("请求成功").setData(loginUser);
        } catch (Exception e) {
            apiResponse.setCode(500).setSuccess(false).setMessage("请求失败");
            e.printStackTrace();
        }
        return apiResponse;
    }

}
